package com.sfpay.coreplatform.order.service.composite.impl;

import java.sql.SQLException;



import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.sfpay.coreplatform.order.common.OrderResponseCode;
import com.sfpay.coreplatform.order.common.enums.CcyType;
import com.sfpay.coreplatform.order.common.enums.OptType;
import com.sfpay.coreplatform.order.common.enums.PayAcctType;
import com.sfpay.coreplatform.order.common.enums.PayType;
import com.sfpay.coreplatform.order.common.enums.RemitMethod;
import com.sfpay.coreplatform.order.common.enums.SourcesCode;
import com.sfpay.coreplatform.order.common.enums.tradeStatus;
import com.sfpay.coreplatform.order.service.composite.common.PayContext;
import com.sfpay.coreplatform.order.service.exception.OrderServiceException;
import com.sfpay.coreplatform.order.valueobject.dto.PayRequest;
import com.sfpay.coreplatform.order.valueobject.dto.Response;
import com.sfpay.coreplatform.order.valueobject.tmo.PayOrder;
@Service
public class PayHandler  extends AbstractHandler {

	private final static Logger logger = LoggerFactory
			.getLogger(PayHandler.class);
	
	@Override
	public Response Handler(PayRequest request, PayContext payContext)throws OrderServiceException {
		Response response = new Response();
		String payNo = "";

		PayOrder payOrder = new PayOrder();
		payNo = this.getPayOrderDao().getNextPayNo();
		payOrder.setOrderNo(payContext.getTradeNo());
		payOrder.setPayNo(payNo);
		payOrder.setOptType(OptType.PAY);
		payOrder.setPayStatus(tradeStatus.PROCESSING);
		payOrder.setPayAmt(request.getPayAmt());
		payOrder.setPayeeAcctNo(request.getPayeeAcctNo());
		payOrder.setPayeeMemberNo(request.getPayerMemberNo());
		payOrder.setTradeTallyCode(request.getTradePostNo());
		payOrder.setPayTallyCode(request.getPayPostNo());
		payOrder.setBusinessSn(request.getOutTradeNo());
		payOrder.setOrderType(request.getOrderType());
		payOrder.setPaySource(SourcesCode.OTHER);
		payOrder.setPayType(PayType.ACCOUNT);
		payOrder.setBusinessSource(SourcesCode.OTHER);
		payOrder.setPaySource(SourcesCode.OTHER);
		payOrder.setPayerAcctType(PayAcctType.ACCOUNT);
		payOrder.setPayerAcctNo(request.getPayerAcctNo());
		payOrder.setPayeeAcctType(PayAcctType.ACCOUNT);
		payOrder.setPayeeAcctNo(request.getPayeeAcctNo());
		payOrder.setRemitMethod(RemitMethod.FAST);
		payOrder.setPayeeCcy(CcyType.RMB);
		payOrder.setPayerCcy(CcyType.RMB);
		payOrder.setRemark("消费");
		try {
			this.getPayOrderDao().addPayOrder(payOrder);
		} catch (Exception e) {
			logger.error(
					" method:payHandle payNo:" + payNo + " save exception ", e);
			if (SQLException.class.isInstance(e.getCause())) {
				throw new OrderServiceException(
						OrderResponseCode.ORDER_DB_EXCEPTION, " oracle errcode "
								+ ((SQLException) e).getErrorCode(), e);
			} else {
				throw new OrderServiceException(
						OrderResponseCode.ORDER_DB_EXCEPTION,
						"method:payHandle payNo:" + payNo + " save exception ",
						e);
			}
		}

		payContext.setPayNo(payNo);
		response.setReturnCode(OrderResponseCode.ORDER_RESPONSE_SUCESS);

		return response;
	}

	@Override
	public String getHandlerName() {
		 
		return "PayHandler";
	}

}
